/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.lhlzi.ithink.model.RecentContact;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.ImageView;

public class IphoneCallLogAdapter extends SimpleAdapter {


	public final static String TAG = IphoneCallLogAdapter.class.toString();
	
	private int mBindingLayout;
	private Context mContext;
	private LayoutInflater mInflater;
	private List<HashMap<String, Object>> mData;
	private ViewBinder mViewBinder;
	private int[] mTo;
	private String[] mFrom;
	
	public IphoneCallLogAdapter(Context context,
			List<HashMap<String, Object>> data, int resource, String[] from,
			int[] to) {
		super(context, data, resource, from, to);
		
		mBindingLayout = resource;
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mData = data;
		mTo = to;
		mFrom = from;
	}
	
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		//super.getView(position, convertView, parent);
		
		if (convertView == null) {
			convertView = (View)mInflater.inflate(mBindingLayout, null);
		}
		
		
		bindView(position, convertView);
		
		TextView textView = (TextView)convertView.findViewById(R.id.displayname);
		CharSequence text = textView.getText();
		if (Utils.isnumber(text))
			textView.setText(Utils.formatNumber(text));
			
		textView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				IphoneCallLogScreen.callNumber(mContext, position);
				//Log.i(TAG, "Name control was clicked,Number=" + getItem(position).getNumber());
			}
			
			
		});
		((ImageView)convertView.findViewById(R.id.detailshow)).setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				
				IphoneCallLogScreen.showDetail(mContext, position);
				//Log.i(TAG, "Array key was clicked,DisplayName=" + getItem(position).getDisplayName());
			}
			
		});
		
		return convertView;
	}
	
	@SuppressWarnings("unchecked")
	private void bindView(int position, View view) {
		final HashMap dataSet = mData.get(position);
		
		if (dataSet == null) return;
		
		final String[] from = mFrom;
		final int[] to = mTo;
		
		final int len = to.length;
		
		for (int i=0; i<len; i++) {
			final View v = view.findViewById(to[i]);
			
			if (v != null) {
				final Object data = dataSet.get(from[i]);
				
				String text = data == null ? "" : data.toString();
				
				if (text == null) text = "";
				
				boolean bound = false;
				
				if (mViewBinder != null) {
					bound = mViewBinder.setViewValue(v, data, text);
				}
				
				if (!bound) {
					if (v instanceof TextView) {
						setViewText((TextView)v, text);
					} else if (v instanceof ImageView) {
						if (data instanceof Integer) {
							setViewImage((ImageView)v, (Integer)data);
						} else {
							setViewImage((ImageView)v, text);
						}
					} else {
						throw new IllegalStateException(v.getClass().getName() + " is not a" +
								" view that can be bounds by this SimpleAdapter");
					}
				}
			}
		}
		
	}
	
	
	
	
	
}
